package cn.jbit.config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/***
 * DruidSource配置类
 * @author Administrator
 *
 */
//读取配置文件
@PropertySource(value= {"classpath:jdbc.properties"})
@Configuration
public class DruidSourceConfig {
	// 获取配置文件中的信息
	@ConfigurationProperties(value="spring.datasource")
	@Bean
	public DataSource druid() {
		return new DruidDataSource();
	}
	// 配置druid的监控   方便后期访问系统监控数据库连接信息
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet());
        // 配置访问druid的监控servlet
        bean.setUrlMappings(Arrays.asList(new String[] { "/druid/*" }));
        Map<String, String> map = new HashMap<String, String>();
        map.put("loginUsername", "admin");
        map.put("loginPassword", "admin");
        map.put("allow", "");
        bean.setInitParameters(map);
        return bean;

    }

    // 配置web监控的filter
    @Bean
    public FilterRegistrationBean setFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
        bean.setUrlPatterns(Arrays.asList(new String[] { "/*" }));
        // 有些东西是不能拦截的，所以要配置exclusions排除掉
        Map<String, String> initParams = new HashMap<>();
        initParams.put("exclusions", "*.js,*.css,*.jpg,*.png,/druid/*");// 这些是不能拦截的
        bean.setInitParameters(initParams);
        return bean;
    }
}
